﻿using ProductDemo.Model;
using DotNetCore.CAP;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Transactions;

namespace ProductDemo.Controllers
{
    [Route("api/Teams")]
    [ApiController]
    public class AggregateController : ControllerBase
    {
        private readonly ICapPublisher capPublisher;
        private readonly AggregateContext aggregateContext;
        public AggregateController(ICapPublisher capPublisher, AggregateContext aggregateContext)
        {
            this.capPublisher = capPublisher;
            this.aggregateContext = aggregateContext;
        }

        /// <summary>
        /// 添加团队和成员信息
        /// </summary>
        /// <param name="value"></param>
        [HttpPost]
        public ActionResult Post(string value)
        {
            using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
            {
                //此处添加业务数据库操作代码

                for (int i = 1; i < 100; i++)
                {

                    Video video = new Video()
                    {
                        VideoUrl = "http://localhost:5365/dsdsdsd.mp4",
                        MemberId = i
                    };
                    capPublisher.Publish<Video>("video.event.post", video, "FailCallBack");
                    //capPublisher.PublishAsync<Video>("video.event.post", video, "FailCallBack");  发放数据的顺序不确定(无序)
                }
                scope.Complete();
            }
            return Ok("添加成功");
        }


        /// <summary>
        /// 回调
        /// </summary>
        /// <param name="value"></param>
        [HttpPost]
        [NonAction]
        [CapSubscribe("FailCallBack")]
        public void FailedCallback(Guid v)
        {
            Console.WriteLine($"接收到回调:{v}");
        }

    }
}
